<template>
  <div>
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>岛主</el-breadcrumb-item>
      <el-breadcrumb-item>登录信息</el-breadcrumb-item>
    </el-breadcrumb> -->
    <el-card>
      <el-row :gutter=" 20">
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple"></div>
        </el-col>
        <el-button type="danger" @click="handleStatDelete">删除</el-button>
      </el-row>
      <el-table :data="logList" border @selection-change="handleSelectionChange">
        <el-table-column type="index" width="50" label="#">
        </el-table-column>
        <el-table-column label="日期" prop="create_time"></el-table-column>
        <el-table-column label="用户" width="100" prop="nick_name"></el-table-column>
        <el-table-column label="IP" prop="ip"></el-table-column>
        <el-table-column type="selection" width="55"></el-table-column>
      </el-table>
      <el-pagination background layout="prev, pager, next" :total='total' :page-size="queryInfo.pagesize" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getLoginLog, deleteLoginLog } from '../../request/apis'
export default {
  data () {
    return {
      queryInfo: {
        pagenum: 1,
        pagesize: 8
      },
      total: 0,
      logList: [],
      selectList: ''
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    getUserList () {
      getLoginLog({ params: this.queryInfo }).then(res => {
        if (res.meta.status !== 200) {
          return this.$message.error('获取登录日志失败')
        }
        this.logList = res.data.logs
        this.total = res.data.total
      })
    },
    handleCurrentChange (newpage) {
      this.queryInfo.pagenum = newpage
      this.getUserList()
    },
    handleSelectionChange (val) {
      const selectList = []
      val.forEach(item => {
        selectList.push(item.id)
      })
      this.selectList = selectList.join(',')
    },
    // 删除记录
    async handleStatDelete () {
      const { data, meta } = await deleteLoginLog({ params: { ids: this.selectList } })
      if (meta.status === 200) {
        this.$message.success('删除成功')
        if ((data.total % this.queryInfo.perpage) === 0) {
          this.queryInfo.pagenum > 1 ? this.queryInfo.pagenum -= 1 : this.queryInfo.pagenum = 1
        }
        this.getUserList()
      }
    }
  }
}
</script>
<style lang="less" scoped>
.item {
  padding: 18px 0;
}

.box-card {
  width: 480px;
}
</style>
